﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UofCASE.AgileProductLinerDSL.Nant.Tasks.Models
{
    public class Feature
    {
        IDictionary<Platform, TestSet> tests;
        public string Name { get; set; }

        public Feature(String name)
        {
            this.Name = name;
            tests = new Dictionary<Platform, TestSet>();
        }

        public void AddToPlatform(Platform p, ITest t)
        {
            if (!tests.ContainsKey(p))
            {
                new TestSet(p, this);
            }
            TestSet set;
            tests.TryGetValue(p, out set);
            set.AddTest(t);
        }

        internal void setFor(TestSet testSet, Platform p)
        {
            TestSet set;
            tests.TryGetValue(p, out set);
            if (set == null)
            {
                tests.Add(p, testSet);
            }
        }

        internal IEnumerable<ITestKey> TestKeys()
        {
            HashSet<ITestKey> set = new HashSet<ITestKey>();
            foreach (TestSet testSet in tests.Values)
            {
                foreach (ITest test in testSet)
                {
                    set.Add(test.Key);
                }
            }
            return set;
        }

        internal ITest Test(ITestKey key, Platform platform)
        {
            TestSet set;
            tests.TryGetValue(platform, out set);
            if (set == null) return null;
            return set.Find(key);
        }

        internal bool Failed(Platform platform)
        {
            return !Passed(platform);
        }

        internal bool Passed(Platform platform)
        {
            TestSet set = TestsFor(platform);
            if (set != null)
            {
                return set.Passed;
            }
            return true;
        }

        internal TestSet TestsFor(Platform p)
        {
            TestSet set;
            tests.TryGetValue(p, out set);
            return set;
        }


        internal int Passed()
        {
            bool passed = true;
            foreach(TestSet set in this.tests.Values){
                passed &= set.Passed;
                if (!passed) break;
            }
            if (passed) return 1;
            return 0;
        }

        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return false;
            }
            Feature other = (Feature)obj;

            return Name.Equals(other.Name);
        }

        // override object.GetHashCode
        public override int GetHashCode()
        {
            return Name.GetHashCode();
        }
    }
}
